package com.brkj.search;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.brkj.communityStudy.QADetailActivity;
import com.brkj.communityStudy.TopicStudyDetailActivity;
import com.brkj.course.CourseGetByIDActivity;
import com.brkj.course.WebViewActivity;
import com.brkj.main.R;
import com.brkj.search.model.DS_SearchResult;
import com.brkj.util.ConstAnts;
import net.tsz.afinal.FinalDb;

import java.util.List;

/**
 * Created by colin.ding on 2014/6/9.
 */

import java.io.Serializable;
import java.util.List;

import android.graphics.BitmapFactory;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import com.brkj.download.CourseDownloader;
import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalDb;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.brkj.model.DS_Course;
import com.brkj.main.R;
import com.brkj.util.ConstAnts;
import com.brkj.util.ImgShow;
import com.brkj.util.TimeHelp;
import com.nostra13.universalimageloader.core.ImageLoader;

public class SearchResultAdapter extends BaseAdapter {
    protected ListItemView listItemView;
    protected LayoutInflater listContainer;
    protected Context context;
    protected List<DS_SearchResult> resultList;

    public SearchResultAdapter(){};

    public SearchResultAdapter(Context context, List<DS_SearchResult> resultList){
        this.context = context;
        this.resultList = resultList;

        listContainer = LayoutInflater.from(context);

    }

    protected static class ListItemView{
        public TextView Name;
        public TextView Content;
        public TextView Time;


    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return resultList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 自定义视图
        listItemView = new ListItemView();
        // 获取list_item布局文件的视图
        if (convertView == null) {
            convertView = listContainer.inflate(R.layout.search_result_item, null);
            // 获取控件对象
            listItemView.Name = (TextView) convertView
                    .findViewById(R.id.Name);
            listItemView.Content = (TextView) convertView
                    .findViewById(R.id.Content);
            listItemView.Time = (TextView) convertView
                    .findViewById(R.id.Time);

            // 设置控件集到convertView
            convertView.setTag(listItemView);
        } else {
            listItemView = (ListItemView) convertView.getTag();
        }

        switch ( resultList.get(position).getType()   ){
            case 0:
                listItemView.Name.setText("课程");
                break;
            case 1:
                listItemView.Name.setText("专题");
                break;
            case 2:
                listItemView.Name.setText("经验分享");
                break;
            case 3:
                listItemView.Name.setText("问答");
                break;
            default:
                listItemView.Name.setText("未知类型");
                break;

        }
        listItemView.Content.setText(resultList.get(position).getBrief());
        listItemView.Time.setText(resultList.get(position).getTime());

        convertView.setOnClickListener(new MyClickListener(position));

        return convertView;
    }

    class MyClickListener implements View.OnClickListener {
        private int position;

        public MyClickListener(int position){
            this.position = position;
        }
        @Override
        public void onClick(View v) {
            switch ( resultList.get(position).getType()   ){
                case 0:
                    Intent intent = new Intent(context, CourseGetByIDActivity.class);
                    intent.putExtra("CourseID", Integer.toString(resultList.get(position).getContentID()));
                    context.startActivity(intent);
                    break;
                case 1:
                    Intent intent2 = new Intent(context, TopicStudyDetailActivity.class);
                    intent2.putExtra("topicID", Integer.toString(resultList.get(position).getContentID()));
                    context.startActivity(intent2);
                    break;
                case 2:
                    Intent intent3 = new Intent(context, WebViewActivity.class);
                    intent3.putExtra("url", resultList.get(position).getURL());
                    context.startActivity(intent3);
                    break;
                case 3:
                    Intent intent4 = new Intent(context, QADetailActivity.class);
                    intent4.putExtra("QAID", Integer.toString(resultList.get(position).getContentID()));
                    context.startActivity(intent4);
                    break;
                default:
                    break;

            }
        }

    }
}
